<?php

?>
<style>
    #etxt_text {
        width: 100% !important;
        height: 400px;
    }
</style>
<div class="hidden" id="etxt-cont">
    <form class="well form-inline margin-horisontal " action="" method="POST" name="etxtForm" id="etxtForm">
        <fieldset>

            <div class="control-group" id="cg-f_etxt_text">
                <label id="l-f_etxt_text" class="control-label" for="f_etxt_text">Текст</label>
                <div class="controls">
                    <textarea id="etxt_text" name="etxt_text" class="input"></textarea>
                </div>
                <span class="help-inline"></span>
            </div>
            

            <div class="form-actions">
                <div class="pull-right">
                    <button class="btn btn-primary" id="etxt-submit"><i class="icon-ok icon-white"></i> Сохранить</button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <button class="btn btn-warning" id="etxt-reset"><i class="icon-repeat icon-white"></i> Сброс</button>
                    <button class="btn btn-danger" id="etxt-cancel"><i class="icon-remove icon-white"></i> Отменить</button>
                </div>
            </div>
        </fieldset>
    </form>
</div>


<script>

    function etxtDialogShow() {
        Elm.etxtCont.dialog('open');
    }

    function etxtDialogHide() {
        Elm.etxtCont.dialog('close');
    }
    
    function etxtDialogClose() {
        Elm.etxtForm.submitCallback = function(data) {};
        Elm.etxtForm.cancelCallback = function(data) {};
        etxtDialogHide();
        return false;
    }
    
    
    function etxtDialogOpen(params, onSubmit, onCancel) {
        params = $.extend({title: 'Изменить текст', text: ''}, params);
        
        Elm.etxtForm.submitCallback = onSubmit || function(){};
        Elm.etxtForm.cancelCallback = onCancel || function(){};
        
        Elm.etxtSource = params;
        $('#etxt_text').val(params.text).keyup();
        
        /*Elm.etxtCont.dialog('option', 'title', params.title);
        Elm.etxtBtnSubmit.attr('disabled', 'disabled');*/
        etxtDialogShow();
    }
    


    $(function() {
        Elm.etxtCont = $('#etxt-cont');
        Elm.etxtForm = $('#etxtForm');
        Elm.etxtForm.submitCallback = function(data) {};
        Elm.etxtForm.cancelCallback = function(data) {};
        Elm.etxtBtnSubmit = $('#etxt-submit');
        
        Elm.etxtSource = {title:'', text:''};
        

        

        Elm.etxtCont.dialog({
            width       : 650,
            modal       : true,
            resizable   : false,
            title       : "",
            autoOpen    : false,
            buttons     : {}
        }).removeClass('hidden');
        Elm.etxtCont.dialog('widget').find('.ui-dialog-titlebar-close').hide();
        
        $('#etxt_text').keyup(function() {
            if ($(this).val() == Elm.etxtSource.text) {
                Elm.etxtCont.dialog('option', 'title', Elm.etxtSource.title);
                Elm.etxtBtnSubmit.attr('disabled', 'disabled');
            }
            else {
                Elm.etxtCont.dialog('option', 'title', Elm.etxtSource.title+'  * ');
                Elm.etxtBtnSubmit.removeAttr('disabled');
            }
        });

        $('#etxt-cancel').click(function(){
            if (typeof(Elm.etxtForm.cancelCallback) == 'function') Elm.etxtForm.cancelCallback();
            etxtDialogClose();
            return false;
        });
        
        $('#etxt-reset').click(function(){
            $('#etxt_text').val(Elm.etxtSource.text).keyup();
            return false;
        });
        
        
        Elm.etxtBtnSubmit.click(function(){
            if (typeof(Elm.etxtForm.submitCallback) == 'function') {
                Elm.etxtForm.submitCallback($('#etxt_text').val());
            }
            return false;
        });
        
        

    });




</script>

<?php /*

Использование:  etxtDialogOpen(params, onSubmit, onCancel);



*/ ?>
